$(function(){




    $("body").on('input','input[name="integral"]',function () {
            var _this = $(this);
            var val = _this.val();
            if(!isNumber(val)){
            _this.val('');
        }
        }).on('input','input[name="inventory"]',function () {
            var _this = $(this);
            var val = _this.val();
            if(!isNumber(val)){
                _this.val('');
            }
    }).on('click',"#courseImage",function () {//封面上传
        //初始化图片上传窗口
        initUpdateFileWin({
            fileType:'img',//文类类型["img","ico","xls","pdf","zip"]
            callback:'uploadCallback',
            cut:true,//开启剪切
            imgSize:DEFAULT_COURSE_IMG_SIZE,//剪切上传后生成图片的规格（开启剪切才起使用）
            aspectRatio:16/9//剪切区域的高宽比例（开启剪切才起使用）
        });
    }).on("click","#save",function () {
        var params = $("#save-form").serializeJSON();
        params = JSON.stringify(params);
        if($("#couponId").val()==='0'){
            icModale(0,'请选择平台商品');
            return;
        }

        $.ajax({
            type:'post',
            url:baseloaction +'/admin/IntegralGood/saveEntity',
            data:params,
            contentType:'application/json;charset=utf-8',
            success:function (result) {
                icModale(0, result.message);
                    if(result.success){
                        window.location.href=baseloaction+"/admin/IntegralGood/listPage";
                    }

            },
            error:function (error) {
                icModale(0, '系统错误，保存课程数据失败');
            }
        });
    }).on("click","#openCoupons",function(){
        parent.layer.open({
            type: 2,
            area: winArea,
            btn: ['选择', '取消'],
            content: baseloaction + "/admin/IntegralGood/selectShopData",
            maxmin: true,
            shade: [0.3, '#000'],
            shadeClose: false,
            title: '选择商品',
            yes: function (index, layero) {
                var _iframe = layero.find("iframe:visible");
                var iframeName = _iframe[0].name;
                var entity = parent.frames[iframeName].getShopData();
                if (entity!=null) {
                    selectCallback(entity.shopDataType,entity.entity);
                    parent.layer.close(index);
                }
            }
        });
    }).on('click','#reomve-coupon',function () {//删除关联的用户
        $("#couponId").val(0);
        $("#couponIdName>span").text('选择后库存由商品的数量决定(课程除外)');
        $("input[name='inventory']").val(0).attr("readonly",false);
        $("input[name='shopType']").val("");
    }).on("ifClicked","input[name='type']",function(){
        var that =$(this);
           var arry =$(".shopData");
        $.each(arry,function(index,object){
            if(that.val()==2){
                $(object).removeClass("hide");
                $("#couponId").val(0);
                $("input[name='shopType']").val("");
                $("input[name='inventory']").val(0).attr("readonly",true).val(0);
            }else{
                $("#couponId").val("");
                $(object).addClass("hide");
                $("input[name='shopType']").val("ENTITY");
                $("input[name='inventory']").val(0).attr("readonly",false).val(0);
            }
        });

    }).on("click",".saveCancel",function(){
        window.location.href=baseloaction+"/admin/IntegralGood/listPage";
    });

//选择用户回调
    function selectCallback(shopDataType,entity) {

        if( shopDataType=="COURSE"){
            var parse = JSON.parse(decodeURIComponent(entity.imageJson));
            $("input[name='imageJson']").val(entity.imageJson);
            $("#material-image").attr("src",parse.pcUrlMap.large);
            $("#file-names").text(parse.fileName);
            $("#create-time").text(parse.createTime);
            $("input[name='inventory']").val(0).attr("readonly",false);
        }else{
            $("input[name='inventory']").val(entity.num).attr("readonly",true);
        }
        $("#couponId").val(entity.id);
        $("input[name='shopType']").val(shopDataType);
        $("#couponIdName>span").text("名称："+entity.name);
        $("#reomve-coupon").removeClass("hide");
    }

    //初始化编辑框
    initSNU({
        id:'context',
        type:1,
        toolsArr:null,
        height:360,
        gs:true,
        video:false
    });

});

/**
 * 图片上传回调
 * @param result
 */
function uploadCallback(result) {
    if(result!=null){
        $("input[name='imageJson']").val(encodeURIComponent(JSON.stringify(result)));
        $("#material-image").attr("src",result.pcUrlMap.large);
        $("#file-names").text(result.fileName);
        $("#create-time").text(result.createTime);
    }
}

